package com.miyake.dsp.osa.lab.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.annotation.Annotation;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.osa.lab.Connector;
import com.miyake.dsp.osa.lab.LabContainer;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
import com.miyake.dsp.osa.lab.SettingPanelListener;

public class CalculatorUiPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3787455048214258691L;

	public LabContainer getContainer() {
		return container;
	}

	private OutletPanelUI outletPanel;
	private LabContainer container;
	private SettingPanelUI settingPanel;
	private JLabel minimizedOutlet;
	private static ImageIcon chartIcon = new ImageIcon("./connector.png");
	
	public CalculatorUiPanel(final LabContainer container, final UiPanelListener listener) {
		this.container = container;
		this.setName(container.getCaption());
		container.setUI(this);
		setBorder(new TitledBorder(new BevelBorder(BevelBorder.RAISED), getAnnotationCaption(container)));
		setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
		JPanel toolPanel = new JPanel();
		toolPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		final JButton visibleButton = new JButton("Minimize");
		visibleButton.setMargin(new Insets(1,1,1,1));
		toolPanel.add(visibleButton);
		
		final JToggleButton bypassButton = new JToggleButton("Bypass");
		bypassButton.setMargin(new Insets(1,1,1,1));
		toolPanel.add(bypassButton);
		bypassButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				container.setBypass(bypassButton.isSelected());
				container.process();
			}
		});
		bypassButton.setSelected(container.isBypass());
		
		JButton removeButton = new JButton("Remove");
		removeButton.setMargin(new Insets(1,1,1,1));
		removeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				listener.onRemove(getThis());
			}
		});
		toolPanel.add(removeButton);
		
		minimizedOutlet = new JLabel(chartIcon);
		//minimizedOutlet.setPreferredSize(new Dimension(100, 24));
		minimizedOutlet.setVisible(false);
		toolPanel.add(minimizedOutlet);
		
		add(toolPanel);
		
		final JPanel contentPanel = new JPanel();
		
		add(contentPanel);
		contentPanel.setLayout(new GridLayout(0,2));
		
		if (container.getSetting() != null) {
			contentPanel.add(settingPanel = getSettingPanelUI(container));
		}
		contentPanel.add(outletPanel = getOutputPanelUI(container, listener));
		
		visibleButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (visibleButton.getText().equals("Maximize")) {
					visibleButton.setText("Minimize");
					contentPanel.setVisible(true);
					minimizedOutlet.setVisible(false);
				}
				else {
					visibleButton.setText("Maximize");
					contentPanel.setVisible(false);
					minimizedOutlet.setVisible(true);
				}
				getParent().repaint();
			}
			
		});
		
		
	}

	protected OutletPanelUI getOutputPanelUI(final LabContainer container,
			final UiPanelListener listener) {
		return new OutletPanelUI(container, listener);
	}

	protected CalculatorUiPanel getThis() {
		return this;
	}

	protected SettingPanelUI getSettingPanelUI(final LabContainer container) {
		return new SettingPanelUI(container.getSetting(), new SettingPanelListener() {
			@Override
			public void onSet() {
				container.process();
			}
		});
	}

	private String getAnnotationCaption(LabContainer comb) {
		return comb.getCaption();
	}

/*	public Point getConnectorLocaltion(Connector connector) {
		return this.outletPanel.getConnectorLocation(connector);
	}*/
	
	public Point getPoint(String key) {
		if (outletPanel.isShowing()) {
			return outletPanel.getPoint(key);
		}
		else if (minimizedOutlet.isShowing()){
			//return minimizedOutlet.getLocationOnScreen();
			return new Point(
					(int)minimizedOutlet.getLocationOnScreen().getX() + minimizedOutlet.getWidth()/2, 
					(int)minimizedOutlet.getLocationOnScreen().getY() + minimizedOutlet.getHeight()/2);
		}
		else {
			return null;
		}
	}

	public Component getConnectorComponent(String key) {
		if (outletPanel.isShowing()) {
			return outletPanel.getConnectorComponent(key);
		}
		else {
			return this.minimizedOutlet;
		}
	}
	
	public void updateData(String key, Connector con) {
		outletPanel.updateData(key, con);
	}

}
